<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template name="form_row">

        <xsl:param name="n"/>
        <xsl:param name="block_class" select="'FormRow'"/>
        <xsl:param name="caption" select="local-name($n)" />
        <xsl:param name="maxlength" select="'255'"/>
        <xsl:param name="option_display_name" />
        <xsl:param name="option_select_id" select="''" />
        <xsl:param name="option_selected_value" />
        <xsl:param name="option_value" />
        <xsl:param name="type" select="'text'"/>

        <div class="{$block_class}">
        <xsl:choose>
            <xsl:when test="boolean($n) = false">[Element Not Found]</xsl:when>

            <xsl:when test="$type = 'checkbox'">
            <label for="{local-name($n)}"><xsl:value-of select="$caption" />:</label>
            <input class="CheckBox" id="{local-name($n)}" type="{$type}" value="{$n}">
                <xsl:if test="(string($n) = 'true') or (string($n) = '1')"><xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
            </input>
            </xsl:when>

            <xsl:when test="$type = 'data_id'">
            <input type="hidden" id="{local-name($n)}" value="{$n}" />
            <label><xsl:value-of select="$caption" />:</label>
            <div class="ReadOnly"><xsl:value-of select="$n" /></div>
            </xsl:when>

            <xsl:when test="$type = 'datepicker'">
            <label for="{concat(local-name($n),'Text')}"><xsl:value-of select="$caption" />:</label>
            <input class="TextBox" id="{concat(local-name($n),'Text')}" maxlength="32" type="{$type}" value="{$n}" />
            <a href="#" id="{local-name($n)}" title="Click to select date and current time…"><img alt="" class="DatePickerIcon" src="./js/songhay/assets/calendar16.jpg" /></a>
            </xsl:when>

            <xsl:when test="$type = 'option'">
            <label for="{local-name($n)}"><xsl:value-of select="$caption" />:</label>
            <select id="{$option_select_id}">
                <option value="">[Not Specified]</option>
                <xsl:for-each select="//*[local-name() = local-name($n)]">
                    <xsl:sort select="./*[local-name() = $option_display_name]"/>
                    <xsl:variable name="option_text">
                        <xsl:choose>
                            <xsl:when test="string-length(./*[local-name() = $option_display_name]) &gt; number($maxlength)">
                                <xsl:value-of select="concat(substring(./*[local-name() = $option_display_name],1,number($maxlength)-1),'…')" />
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="./*[local-name() = $option_display_name]" />
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
                <xsl:if test="(string-length(./*[local-name() = $option_value]) &gt; 0) and (string-length(./*[local-name() = $option_display_name]) &gt; 0)">
                <option value="{./*[local-name() = $option_value]}"><xsl:if test="($option_selected_value = (./*[local-name() = $option_value]))"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if><xsl:value-of select="$option_text" /></option>
                </xsl:if>
                </xsl:for-each>
            </select>
            </xsl:when>

            <xsl:when test="$type = 'readonly'">
            <label for="{local-name($n)}"><xsl:value-of select="$caption" />:</label>
            <div class="ReadOnly"><xsl:value-of select="$n" /></div>
            </xsl:when>

            <xsl:when test="$type = 'text'">
            <label for="{local-name($n)}"><xsl:value-of select="$caption" />:</label>
            <input class="TextBox" id="{local-name($n)}" maxlength="{$maxlength}" type="{$type}" value="{$n}" />
            </xsl:when>

            <xsl:when test="$type = 'textarea'">
            <label for="{local-name($n)}"><xsl:value-of select="$caption" />:</label>
            <textarea class="TextArea" id="{local-name($n)}"><xsl:choose><xsl:when test="string-length($n) = 0">&#160;</xsl:when><xsl:otherwise><xsl:value-of select="$n"/></xsl:otherwise></xsl:choose></textarea>
            </xsl:when>

            <xsl:when test="$type = 'textarea_node'">
            <label for="{local-name($n)}"><xsl:value-of select="$caption" />:</label>
            <textarea class="TextArea" id="{local-name($n)}"><xsl:choose><xsl:when test="string-length($n) = 0">&#160;</xsl:when><xsl:otherwise><xsl:copy-of select="$n/node()"/></xsl:otherwise></xsl:choose></textarea>
            </xsl:when>

        </xsl:choose>
        </div>
    </xsl:template>

</xsl:stylesheet> 

